package com.zenika.training.wicket.zencontact.pages;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.apache.wicket.validation.validator.StringValidator;

import wicket.extensions.markup.html.datepicker.PopupDatePicker;

import com.zenika.training.wicket.zencontact.ZenContactApplication;
import com.zenika.training.wicket.zencontact.model.Contact;

public class EditContactPage extends ZenContactBasePage {

	public EditContactPage() {
		setModel(new CompoundPropertyModel(new LoadableDetachableModel() {
			protected Object load() {
				return new Contact();
			}
		}));
		init();
	}

	public EditContactPage(final Long contactId) {
		setModel(new CompoundPropertyModel(new LoadableDetachableModel() {
			protected Object load() {
				return ZenContactApplication.getApp().getContactDao().get(contactId);
			}
		}));
		init();
	}

	private void init() {
		add(new FeedbackPanel("feedback"));
		add(new ContactForm("form", getModel()));
	}

	protected String getPageTitle() {
		return "Edit contact";
	}

	private class ContactForm extends Form {

		public ContactForm(String id, IModel m) {
			super(id, m);
			
			TextField firstName = new TextField("firstName");
			firstName.setRequired(true);
			firstName.add(StringValidator.maximumLength(15));
			add(firstName);

			TextField lastName = new TextField("lastName");
			lastName.setRequired(true);
			lastName.add(StringValidator.maximumLength(20));
			add(lastName);

			// Add departure date component
			RequiredTextField dateField = new RequiredTextField("birthDate", Date.class);
			add(dateField);
			add(new PopupDatePicker("datePickerField", dateField));

			TextField email = new TextField("email");
			email.add(EmailAddressValidator.getInstance());
			add(email);

			TextArea notes = new TextArea("notes");
			notes.add(StringValidator.maximumLength(500));
			add(notes);

			CheckBox active = new CheckBox("active");
			add(active);

			RadioChoice category = new RadioChoice("category") {
				protected boolean localizeDisplayValues() {
					return true;
				}
			};
			category.setRequired(true);
			category.setSuffix("&nbsp;");
			category.setChoices(new AbstractReadOnlyModel() {
				public Object getObject() {
					List l = new ArrayList(3);
					l.add("Professional");
					l.add("Personal");
					return l;
				}
			});
			add(category);

			DropDownChoice language = new DropDownChoice("language") {

				protected boolean localizeDisplayValues() {
					return true;
				}
			};
			language.setChoices(new AbstractReadOnlyModel() {
				public Object getObject() {
					List l = new ArrayList();
					l.add("German");
					l.add("English");
					l.add("Spanish");
					l.add("French");
					l.add("Italian");
					return l;
				}
			});
			add(language);

			add(new Button("save", new ResourceModel("button.save")) {
				public void onSubmit() {
					Contact c = (Contact) getForm().getModelObject();
					ZenContactApplication.getApp().getContactDao().save(c);
					setResponsePage(ListContactsPage.class);
				}
			});
			add(new Button("cancel", new ResourceModel("button.cancel")) {
				public void onSubmit() {
					setResponsePage(ListContactsPage.class);
				}
			}.setDefaultFormProcessing(false));
		}
	}
}
